from twisted.internet import reactor
from twisted.web import server
from twisted.web import static
from twisted.web.resource import Resource
from pyamf.remoting.gateway.twisted import TwistedGateway

from gateway.associate.associate_gateway import AssociateGateway
from gateway.classify.classify_gateway import ClassifyGateway
from gateway.cluster.cluster_gateway import ClusterGateway
from common import config
from resource.rootResource import RootResource


class Server:
    services = {"ClusterGateway": ClusterGateway(),
                "ClassifyGateway": ClassifyGateway(),
                "AssociateGateway": AssociateGateway()}
    
    def __init__(self):
        gateway = TwistedGateway(self.services, expose_request=False)
        self.root = Resource()
        self.root.putChild("", RootResource())
        self.root.putChild("gateway", gateway)
        self.root.putChild("flex", static.File("flex/"))
        self.root.putChild("crossdomain.xml", static.File("flex/crossdomain.xml"))

    def start_server(self):
        reactor.listenTCP(config.SERVER_PORT, server.Site(self.root))
        reactor.run()
